package com.mls.fivedistrict.controller.portal;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Goods;
import com.mls.fivedistrict.server.GoodsService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @ author chenhua
 * @ date 2018/5/16 15:06
 */
@RestController
@RequestMapping("/api/app")
public class GoodsController extends BaseController {
    @Autowired
    GoodsService goodsService;

    /**
     * 分頁查詢
     * @return
     */
    @GetMapping("/v1/goodses")
    public ServiceResponse getGoodsList(){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(),pageFilter.getPageSize());
        List selectList = goodsService.selectList(pageFilter.getWrapper());
        PageInfo<Goods> goodsPageInfo = new PageInfo<Goods>(selectList, 5);
        return ServiceResponse.createSuccessResponse(goodsPageInfo);
    }

    /**
     * 根据id查询详情
     * @param goodsId
     * @return
     */
    @GetMapping("/v1/goods/{goodsId}")
    public ServiceResponse getGoodsById(@PathVariable Long goodsId){
        Optional.ofNullable(goodsId).orElseThrow(()->new CustomerException("要查询的商品id不能为空"));
        Goods goods = goodsService.selectById(goodsId);
        if(null!=goods){
            return ServiceResponse.createSuccessResponse(goods);
        }else{
            return  ServiceResponse.createErrorResponse("商品不存在");
        }
    }


}
